package cn.yitun.chat.yitunserverimapicore.business.chat.service.impl;

import cn.yitun.chat.yitunservercommonlib.dataentity.data.base.chat.ChatItem;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Content;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.UserChatContentDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserChatContentVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunserverimapicore.business.chat.service.UserChatService;
import cn.yitun.chat.yitunserverimapicore.mq.UserChatQueue;
import cn.yitun.chat.yitunserverimcore.bridge.main.common.session.SocketSessionStore;
import cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager.ChatManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.chat.UserChatPush;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Description <br>
 * Date 2019-06-28 14:53:30<br>
 *
 * @author yitun<br>
 * @version 1.1
 */
@Service
@Slf4j
public class UserChatServiceImpl implements UserChatService{

//	@Resource
//	UserManager userManager;
	@Resource
	ChatManager chatManager;
	@Resource
	UserChatPush userChatPush;
	@Resource
	SocketSessionStore socketSessionStore;
//	@Resource
//	LastChatManager lastChatManager;
//	@Resource
//	UserChatManager userChatManager;
	@Resource
	UserChatQueue userChatQueue;

	/**
	 * 
	 * Date 2019-02-17 12:12:36<br>
	 * Description 用户对用户聊天
	 * 
	 * @param messageId
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	public void userChat(Long messageId, UserChatContentDto query) {
		Content content = query.getContent();
		Long sendUserId = query.getSendUserId();
		Long receiveUserId = query.getReceiveUserId();
		if (NumUtil.isInValid(content.getKey())) {
			content.setKey(messageId);
		}
		List<ChatItem> chatItems = chatManager.wordsFilter(content);
		boolean send = socketSessionStore.hasSession(receiveUserId);
		userChatPush.pushUserChat(messageId, sendUserId, receiveUserId, content);
		userChatQueue.add(sendUserId, receiveUserId, content, chatItems, send);
	}

	/**
	 * 聊天记录 计数
	 * @param query
	 * @return
	 */
	@Override
	public int queryCount(UserChatContentDto query) {
		return chatManager.queryUserChatCount(query);
	}

	/**
	 * 聊天记录 列表
	 * @param query
	 * @return
	 */
	@Override
	public List<UserChatContentVo> queryList(UserChatContentDto query) {
		return chatManager.queryUserChatList(query);
	}

	/**
	 * 获取详情
	 * @param id
	 * @return
	 */
	@Override
	public UserChatContentVo getDetail(Long id) {
		return chatManager.getUserChatContent(id);
	}

	/**
	 * 详情列表
	 * @param query
	 * @return
	 */
	@Override
	public List<UserChatContentVo> listDetail(UserChatContentDto query) {
		return chatManager.listUserChatContent(query);
	}


	/**
	 * 通过查询方向和点获取消息记录
	 * @param query
	 * @return
	 */
	@Override
	public List<UserChatContentVo> getListByStartId(UserChatContentDto query) {
		return chatManager.getUserChatListByStartId(query);
	}

	/**
	 * 通过查询方向和消息key获取消息记录
	 * @param query
	 * @return
	 */
	@Override
	public List<UserChatContentVo> getListByStartMessageKey(UserChatContentDto query) {
		return chatManager.getUserChatListByStartMessageKey(query);
	}
}
